/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName CustomListItemRenderer.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.views.renderers
{

	import flash.display.CapsStyle;
	import flash.display.Shape;
	import flash.geom.Matrix;
	
	import mx.core.IDataRenderer;
	import mx.core.mx_internal;
	import mx.events.EffectEvent;
	import mx.events.PropertyChangeEvent;
	import mx.graphics.BitmapScaleMode;
	import mx.styles.CSSStyleDeclaration;
	
	import spark.components.LabelItemRenderer;
	import spark.components.supportClasses.StyleableTextField;

	use namespace mx_internal;


	/**
	 * Constructor
	 */
	
	public class CustomListItemRenderer extends LabelItemRenderer implements IDataRenderer
	{

		//
		// Constants
		//
		public static const H_GAP:int=4;

		//
		// UI Bits
		//
		protected var phoneDisplay:StyleableTextField;


		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------

		/**
		 * itemContent
		 * 
		 */
		private var _itemContent:Object;

		[Bindable]
		public function get itemContent():Object
		{
			
			return _itemContent;
			
		}

		public function set itemContent(value:Object):void
		{
			
			_itemContent=value;

			if (labelDisplay)
				labelDisplay.text= value.name;

			phoneDisplay.text= itemContent.contact;

		}

		/**
		 * data
		 * Required by IDataRenderer
		 */
		
		override public function set data(value:Object):void
		{
			
			super.data=value;

			if (data)
				itemContent= value;
			
		}

		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		public function CustomListItemRenderer()
		{
			
			super();
			
		}

		//--------------------------------------------------------------------------
		//
		//  Overridden methods: LabelItemRenderer
		//
		//--------------------------------------------------------------------------

		// The message field is used to display the name of the it.  Customize its 
		// appearance by overriding its creation methods
		override protected function createLabelDisplay():void
		{
			
			labelDisplay=StyleableTextField(createInFontContext(StyleableTextField));
			labelDisplay.styleName=this;
			labelDisplay.editable=false;
			labelDisplay.selectable=false;
			labelDisplay.cacheAsBitmap=true;
			labelDisplay.multiline=true;
			labelDisplay.wordWrap=true;

			if (data && data.label )
				labelDisplay.text=data.label;

			addChild(labelDisplay);
			
		}

		override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void
		{
			
			if (!(selected || down))
			{
				
				graphics.beginFill(0xefefef);
				graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
				graphics.endFill();
				
			}
			else
			{
				
				graphics.beginFill(0xfafafa);
				graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
				graphics.endFill();
				
			}

			// Draw our own color divider
			graphics.lineStyle(2, 0xFFFFFF, 1, false, "normal", CapsStyle.NONE);
			graphics.moveTo(0, unscaledHeight - 1);
			graphics.lineTo(unscaledWidth, unscaledHeight - 1);
			
		}

		override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void
		{
			
			// Update styles on textFields
			labelDisplay.commitStyles();
			phoneDisplay.commitStyles();

			// labelDisplay
			var labelHeight:Number=getElementPreferredHeight(labelDisplay);
			var labelY:Number = (unscaledHeight/2) - (labelHeight/2);
			setElementSize(labelDisplay, unscaledWidth/2, labelHeight);
			setElementPosition(labelDisplay, H_GAP, labelY);

			// phoneDisplay
			var phoneHeight:Number=getElementPreferredHeight(phoneDisplay);
			setElementSize(phoneDisplay, unscaledWidth/2, phoneHeight);
			setElementPosition(phoneDisplay, unscaledWidth - getElementPreferredWidth(phoneDisplay) - H_GAP, labelY);


			// Invailidate the size so that we can revalidate text due to potential reflow
			if (labelHeight != getElementPreferredHeight(labelDisplay))
				invalidateSize();
			
		}

		//--------------------------------------------------------------------------
		//
		//  Overridden methods: UIComponent
		//
		//--------------------------------------------------------------------------

		override protected function createChildren():void
		{
			
			super.createChildren();

			if (!phoneDisplay)
			{
				
				phoneDisplay=StyleableTextField(createInFontContext(StyleableTextField));
				phoneDisplay.styleName=this;
				phoneDisplay.editable=false;
				phoneDisplay.selectable=false;
				phoneDisplay.multiline=false;
				phoneDisplay.wordWrap=false;
				phoneDisplay.cacheAsBitmap=true;

				addChild(phoneDisplay);

				// Set data if avaiable
				if (itemContent && itemContent.phNo)
				{
					
					phoneDisplay.text=itemContent.phNo;
					
				}
				
			}

		}
		
		override protected function measure():void
		{
			
			var calculatedHeight:Number=getElementPreferredHeight(labelDisplay);

			// Round
			calculatedHeight=Math.floor(calculatedHeight);

			// Minimum height of 14 pixels
			measuredHeight=Math.max(calculatedHeight, 14);

			// Measured width
			measuredWidth=getElementPreferredWidth(labelDisplay) + getElementPreferredWidth(phoneDisplay);

			// Minimums , Hard code in the measuredMinWidth
			measuredMinWidth=310;
			measuredMinHeight=measuredHeight;
			
		}
		
	}
	
}
